package com.example.entity.restTemplate;

import com.fasterxml.jackson.databind.ser.std.StdKeySerializers;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpMethod;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.ResponseErrorHandler;

import java.io.IOException;
import java.net.URI;
import java.util.Scanner;

/**
 * @Author yudong
 * @Date 2023/3/1 17:40
 * @description：
 */
@Slf4j
public class MyResponseErrorHandler extends DefaultResponseErrorHandler {

    @SneakyThrows
    @Override
    public void handleError(URI url, HttpMethod method, ClientHttpResponse response) throws IOException {
        // 里面可以实现你自己遇到了Error进行合理的处理
        Scanner scanner = new Scanner(response.getBody()).useDelimiter("\\A");
        String body = scanner.hasNext() ? scanner.next() : "";
        log.info("请求失败，异常信息 {}", body);


        throw new Exception(body);

    }

}
